<template>
  <div class="main-search" v-loading="load">
      <div style="display: flex;margin:10px auto">
          <span style="margin:0 auto">{{title}}</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px" label-position="left">
        <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item v-if="urlType == 'add'" label="密码">
            <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="姓名">
            <el-input v-model="form.true_name"></el-input>
        </el-form-item>
        <el-form-item label="用户编码" v-if="this.info.type == 'normal'">
            <el-input v-model="form.user_code"></el-input>
        </el-form-item>
        <el-form-item label="部门">
            <!-- <el-select v-model="form.region" placeholder="请选择部门">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
            </el-select> -->
            <div style="height:200px;border: 1px solid rgb(220,220,220);border-radius:5px">
                <el-scrollbar style="height: 100%;">
                    <el-tree :data="sideList" check-strictly @check-change="getDepartment" node-key="id" show-checkbox :default-checked-keys="form.did"  :default-expand-all="true" :props="Props" @node-click="handleNodeClick"></el-tree>
                </el-scrollbar>
                
            </div>
        </el-form-item>
        <el-form-item label="角色">
            
            <div>
                <el-select v-model="form.oid"  multiple placeholder="请选择" >
                    <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                        >
                    </el-option>
                </el-select>
                <el-checkbox style="margin-left:10px" v-model="form.iswoker">员工</el-checkbox>
            </div>
        </el-form-item>
        <el-form-item label="性别" v-if="this.info.type == 'normal'">
            <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
            <el-option label="未知" value="2"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="手机号" >
            <el-input v-model="form.phone"></el-input>
        </el-form-item>
         <el-form-item v-if="urlType =='add'">
            <el-button type="primary" @click="onSubmit(0)">保存并返回</el-button>
            <el-button type="primary" @click="onSubmit(1)">保存继续添加</el-button>
        </el-form-item>
        <el-form-item v-else>
            <el-button type="primary" @click="onSubmit(0)">保存</el-button>
            <el-button @click="returns">返回</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import $ from "jquery";
export default {
    props:{
        sideList:Array,
        info:Object
    },
    data() {
      return {
          load:true,
        form: {
            username:'',
            password:'',
            true_name:'',
            department:[],
            oid:[],
            did:[],
            iswoker:true,
            user_code:'',
            sex:"0",
            phone:'',
        },
        Props:{label:'name'},
        options: [],
        defaultKey:[],
        urlType:'',
        title:'',
        id:this.$route.query.id,


      }
    },
    mounted:function(){
        console.log(this.id)
        this.load = true
        setTimeout(()=>{
            this.load = false
            this.urlType = this.$route.query.editType
            if(this.urlType == 'edit'){
                this.load = true
                this.title = "用户信息"
                this.getUserInfo();
            }else if(this.urlType == 'add'){
                this.title = "新建用户"
                this.form.did = [this.id]
                // this.form.department = [this.id]
                console.log( this.form.did)

            }
            this.getRoles();
        },1000)
        

        
        // console.log(this)
        
    },
    methods:{
        onSubmit(type){
            console.log(this.form)
            // return;
            this.load = true
            if(this.urlType == 'edit'){
                if(this.form.username == ''){
                    this.$message.error('用户名不能为空');
                    return;
                }
                $.post(this.info.editUser,{info:JSON.stringify(this.form)},Response => {
                    if(Response.success){
                        // this.options = Response.data;
                        this.load = false
                        this.$message.success('保存成功');
                    }else{
                        this.$message.error(Response.errMessage);
                    }
                    // console.log(Response)
                })
            }else if(this.urlType == "add"){
                if(this.form.username == ''){
                    this.$message.error('用户名不能为空');
                    this.load = false
                    return;
                }
                // 是否为简易版创建
                this.form.isEasy = 1
                $.post(this.info.newUser,{info:JSON.stringify(this.form)},Response => {
                    if(Response.success){
                        this.load = false
                        this.$message.success('创建成功');
                        if(!type){
                            this.$router.go(-1)
                        }else{
                            // 重置属性
                            this.form.username = ""
                            this.form.password = ""
                            this.form.true_name = ""
                            this.form.user_code = ""
                            this.form.phone = ""
                            this.form.sex = "0"

                            this.getRoles();
                            console.log("继续")
                        }
                        
                    }else{
                        this.$message.error(Response.errMessage);
                    }
                    // console.log(Response)
                })
                // console.log("添加用户 ")
            }
            
        },
        returns(){
            // console.log(this.$route.query.search)
            if(this.$route.query.search){
                console.log(this.$route.params.searchInfo)
                this.$router.replace({name:"userTable",params:{'type':this.info.type,"currentPage1":this.$route.params.currentPage1,'editType':'search',search:this.$route.params.searchInfo}})
            }else{
                this.$router.go(-1)
            }
            
        },
        getRoles(){
            // /api/v20/role/list
            $.get(this.info.roleList,{},Response => {
                    if(Response.success){
                        this.options = Response.data;
                        if(this.urlType == "add"){
                            this.options.forEach(k=>{
                                // console.log(k)
                                if(k.name == '基础角色'){
                                    this.form.oid = []
                                    this.form.oid.push(k.id)
                                }
                            })
                        }
                        
                        // this.form.oid = ['1060026212534976512']
                    }else{
                        this.$message.error(Response.errMessage);
                    }
                    // console.log(Response)
                })
        },
        getUserInfo(){
            // /api/v20/user/getUserInfo
            // console.log(this.id)
            $.get(this.info.getUserInfo,{id:this.id},Response => {
                    if(Response.success){
                        // Response.data.forEach(element => {
                        //     // console.log(element.oid.split(","))
                            
                        // });
                        Response.data[0].iswoker = parseInt(Response.data[0].iswoker) == 1 ? true : false
                        this.form = Response.data[0];
                        console.log(this.form)
                        delete this.form.orgs
                        if((this.form.did || []).indexOf(",") != -1){
                            // console.log(this.form.did.split(","))
                            // 多个
                            this.form.did = this.form.did.split(",")
                            this.sideList.forEach(element => {
                                this.defaultKey.push(Number(element))
                            });
                        }else{
                            // 单个
                            let id = this.form.did
                            this.form.did = []
                            this.form.did.push(id)

                            
                        }
                        if((this.form.oid || []).indexOf(",") != -1){
                            this.form.oid = this.form.oid.split(",")
                        }else{
                            let id = this.form.oid
                            this.form.oid = []
                            this.form.oid.push(id)
                        }
                    }else{
                        this.$message.error(Response.errMessage);
                    }
                    this.load = false
                    
                })
        },
        getDefaultKey(ele,id){
            ele.forEach(E=>{
                // console.log(id)
                if(E.id == id){
                    console.error(E)
                    this.defaultKey.push(E)
                    // break;
                }else{
                    if(E.children != null){
                        this.getDefaultKey(E.children,id)
                    }
                }
            })
        },
        getDepartment(data, checked, indeterminate){
            console.log(data, checked, indeterminate)
            if(checked){
                // 增加节点数据
                if(this.form.department == null){
                    this.form.department = []
                }
                // console.log(this.form.department)
                this.form.department.push(data.id)
            }else{
                // 删除
                var index = (this.form.department || []).indexOf(data.id);
                if(index > -1){
                    this.form.department.splice(index,1)
                }
                // console.log(this.form.department)
            }
        },
        handleNodeClick(){

        },
        loading(bool = true) {
                
            const loading = this.$loading({
            lock: true,
            text: '',
            spinner: 'el-icon-loading',
            background: 'rgba(255, 255, 255, 0.7)'
            });
            if(!bool){
                loading.close();
            }
        }
    }
}
</script>

<style lang="less">
    .main-search{
        /* margin-top: 8rem; */
        width: 30rem;
        margin:0 auto;
        overflow-y:auto;
        .el-scrollbar__wrap{
            overflow-x:hidden;
        }
    }
</style>